package com.qixin.web.controller.TencentMap;


import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.ComStationinfo;
import com.qixin.api.bean.ComStationinfoPicture;
import com.qixin.api.bean.StationInfo;
import com.qixin.api.bean.pojo.ChargingInfo;
import com.qixin.api.service.ComStationinfoPictureService;
import com.qixin.api.service.ComStationinfoService;
import com.qixin.api.serviceimpl.ComStationinfoPictureServiceImpl;
import com.qixin.util.chargeInterfce.QiXinPublic.StarInterFce;

import com.qixin.web.bean.ConnectorStatusInfos;
import com.qixin.web.bean.StatuInfos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充电站信息
用于描述充电站的信息，包含充电站的基本信息、服务信息、支付信息等 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-21
 */
@Controller
    @RequestMapping("/weStationinfo")
public class WeStationinfoController {
    @Autowired
    ComStationinfoService comStationinfoService;
    @Autowired
    ComStationinfoPictureService comStationinfoPictureService;
    /**
     * 查询接口中的充电站信息  无用
     * @return
     */
/*
    @ResponseBody
    @RequestMapping("/findStationInfo")
    public StationInfo findStationInfo ( ){
     return    comStationinfoService.findStationInfo(1,1000);
    }
*/

    /**
     * 把接口信息添加到数据库中
     * @return
     */

    @ResponseBody
    @RequestMapping("/dateInsert")
    public boolean dateInsert (){
        try {
            return    comStationinfoService.dateInsert();
        } catch (SQLException e) {
            e.printStackTrace();
            return  false;
        }
    }

    /**
     * 跳转搜索充电桩主页面
     * @return
     */
    @RequestMapping("main.html")
    public String findNearbyStation ( ){
        return  "/Map/searchCharge";
    }
    /**
     * 跳转搜索充电桩主页面
     * @return
     */
    @RequestMapping("/map.html")
    public String findNearbyStationForMap ( ){

        return  "/Map/searchChargeByMap";
    }
    /**
     * 定位后 查询附近最近的十个充电站
     * @return
     */
    @ResponseBody
    @RequestMapping("/findNearbyStation")
    public List<ComStationinfo> findNearbyStation (String lat, String lng,String param){
 return  comStationinfoService.findNearbyStation(lat,lng,param);
    }


    /**
     * 跳转充电站详细信息
     * @return
     */
    @RequestMapping("chargingInfo.html")
    public String findNearbyStation (String stationID,Long id,String lat,String lng, Model model,String distance){
        //图片
        Map map=new HashMap<>();
        map.put("relaction_id",id);
        model.addAttribute("image", comStationinfoPictureService.selectByMap(map));
       //充电站信息和充电桩信息
       List<ChargingInfo> chargingInfos= comStationinfoService.findChargeInfo(id);
        //循环切割出行
        String[]fees = new String[]{};
        for (int i = 0; i < chargingInfos.size(); i++) {
            fees=  chargingInfos.get(i).getElectricityFee().split(";");
        }
        //查出来的是 一个充电站的信息
       List<ChargingInfo> chargingInfos1= comStationinfoService.findChargeInfo(id);

       StarInterFce starInterFce=new StarInterFce();
        //调用接口  准备空闲状态的关联
      String result=   starInterFce.getChargState(starInterFce.getToken(),new String[]{chargingInfos1.get(0).getStationID()} );
          //接口查出的数据
        StatuInfos resultStationStatu=  JSONObject.parseObject(result,StatuInfos.class);
        List<ConnectorStatusInfos> connS=resultStationStatu.getStationStatusInfos().get(0).getConnectorStatusInfos();
         //循环把接口查出的状态  给 从数据库查出的每个 充电枪
        for (int i = 0; i < chargingInfos1.size(); i++) {
            for (int j = 0; j < connS.size(); j++) {
                if(chargingInfos.get(i).getConnectorID().equals(connS.get(j).getConnectorID())){
                    chargingInfos1.get(i).setStatus(connS.get(j).getStatus());
                    chargingInfos1.get(i).setLockStatus(connS.get(j).getLockStatus());
                    chargingInfos1.get(i).setParkStatus(connS.get(j).getParkStatus());
                }
            }
        }
        
        model.addAttribute("chargingInfo",chargingInfos1);
        model.addAttribute("lat",lat);
        model.addAttribute("lng",lng);
        model.addAttribute("distance",distance);
        model.addAttribute("fees",fees);
        model.addAttribute("chargeID",id);
        return  "/Map/chargeInfo";
    }

	
}
